package com.meixiang.fragment.service;


import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.service.CosmetologistListAdapter;
import com.meixiang.entity.services.PersonalDetail;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 美容师 个人资料
 */
public class PersonalDataFragment extends BaseFragment {


    @Bind(R.id.tv_Certification)
    TextView tvCertification;
    @Bind(R.id.tv_age)
    TextView tvAge;
    @Bind(R.id.tv_hobby)
    TextView tvHobby;
    @Bind(R.id.tv_hometown)
    TextView tvHometown;
    @Bind(R.id.tv_explain)
    TextView tvExplain;
    @Bind(R.id.tv_explain_content)
    TextView tvExplainContent;
    @Bind(R.id.tv_check_all)
    TextView tvCheckAll;
    @Bind(R.id.recycler)
    RecyclerView recycler;

    private PersonalDetail personalDetail;
    CosmetologistListAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_personal_data, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        recycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        adapter=new CosmetologistListAdapter(this);
        recycler.setAdapter(adapter);
        getData();
    }

    @Override
    protected void setData() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getData() {
        HttpParams params = new HttpParams();
        params.put("beauticianId", getActivity().getIntent().getStringExtra("beauticianId"));
        HttpUtils.post(Config.PERSONAL_DETAIL, params, new HttpCallBack(getActivity()) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                personalDetail = AbJsonUtil.fromJson(result.toString(), PersonalDetail.class);
                tvCertification.setText(personalDetail.getOccupation());
                tvAge.setText(personalDetail.getAge() + "岁");
                tvHobby.setText(personalDetail.getHobby());
                tvHometown.setText(personalDetail.getHometown());
                tvExplainContent.setText(personalDetail.getPersonalDesc());
                if (tvExplainContent.getLineCount() > 2) {
                    tvExplainContent.setMaxLines(2);
                    tvCheckAll.setVisibility(View.VISIBLE);
                } else {
                    tvCheckAll.setVisibility(View.GONE);
                }
                adapter.addUrls(personalDetail.getPhotoImageMore());
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(activity,message);
            }
        });
    }

    /**
     * 旋转180度
     *
     * @param context
     * @param resId
     * @return
     */
    private BitmapDrawable rotateImage(Context context, int resId) {
        Matrix matrix = new Matrix();
        matrix.postRotate(180);
        Bitmap resources = BitmapFactory.decodeResource(context.getResources(), resId);
        Bitmap bitmap = Bitmap.createBitmap(resources, 0, 0, resources.getWidth(), resources.getHeight(), matrix, true);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
        return bitmapDrawable;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @OnClick(R.id.tv_check_all)
    public void onClick() {
        if (tvExplainContent.getMaxLines() == 2) {
            tvCheckAll.setText("收起");
            tvExplainContent.setMaxLines(tvExplainContent.getLineCount());
            BitmapDrawable drawable = rotateImage(getActivity(), R.mipmap.detial);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvCheckAll.setCompoundDrawables(null, null, drawable, null);
        } else {
            tvCheckAll.setText("查看全部");
            tvExplainContent.setMaxLines(2);
            Drawable drawable = ContextCompat.getDrawable(getActivity(), R.mipmap.detial);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvCheckAll.setCompoundDrawables(null, null, drawable, null);
        }
    }
}
